from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
import json

# Create your views here.
from books.models import BookInfo


class BooksView(View):

    def post(self, request):
        """
           保存图书
       :param request:
       :return:
        """
        # 1、前端传递保存图书的数据 json接受
        json_data = request.body.decode()
        dict_data = json.loads(json_data)
        btitle = dict_data.get('btitle')
        bpub_date = dict_data.get('bpub_date')
        bread = dict_data.get('bread')
        # 2、验证数据
        if not all([btitle, bpub_date]):
            return JsonResponse({'code': '400'}, status=400)

        # 3、保存数据
        book = BookInfo.objects.create(btitle=btitle, bpub_date=bpub_date)
        # 4、返回结果
        return JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date,
            'bread': book.bread
        })

    def get(self, request):
        """
                获取图书
               :param request:
               :return:
        """
        books = BookInfo.objects.all()
        book_list = []
        for book in books:
            book_list.append({
                'id': book.id,
                'btitle': book.btitle,
                'bpub_date': book.bpub_date,
                'bread': book.bread
            })

        return JsonResponse({'books': book_list})


class BookView(View):

    def delete(self, request, id):
        """
             删除一本图书
         :param request:
         :param pk: 图书id
         :return:
         """
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400'}, status=400)

        # 逻辑删除
        book.is_delete = True
        book.save()
        # 物理删除
        # book.delete()
        return JsonResponse({})

    def put(self, request, id):
        """
            更新一本图书
        :param request:
        :param pk: 图书id
        :return:
        """
        # 查询更新的图书
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400', 'message': '数据不存在'}, status=400)
        # 获取更新的字段
        json_data = request.body.decode()
        dict_data = json.loads(json_data)
        btitle = dict_data.get('btitle')
        bpub_date = dict_data.get('bpub_date')
        bread = dict_data.get('bread')

        # if btitle:
        #     book.btitle=btitle
        # if bpub_date:
        #     book.bpub_date=bpub_date
        # if bread:
        #     book.bread=bread
        # book.save()
        BookInfo.objects.filter(id=id).update(**dict_data)
        book = BookInfo.objects.get(id=id)

        return JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date
        })

    def get(self, request, id):
        """
            获取一本图书
        :param request:
        :param pk:  图书id
        :return:
        """
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400', 'message': '数据不存在'}, status=400)

        return JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date,
            'bread': book.bread
        })
